import {getDictDataOption} from '@/utils/getDictDataOption'
import {getUserGridTree} from '@/utils/SessionDataUtil'
import {findCommunityList, findBuildingList, findUnitList, findHouseList, getHouse, findCourtyardList} from '@/http/FangWuGuanLiApi'

export const getFormInfo = (data, tableName) => {
  data = resetData(data, tableName)
}
const getHouseInfo = async id => {
  return new Promise(resolve => {
    getHouse(id).then(resp => {
      resolve(resp)
    })
  })
}
const resetData = async (data, tableName) => {
  let newData = data
  if (newData.liveHouseId && newData.liveHouseId !== '') {
    let res = await getHouseInfo(newData.liveHouseId)
    if (res.code && res.code === '200') {
      newData.liveCommunityId = res.data.communityId
      newData.liveBuildingId = res.data.buildingId
      newData.liveUnitId = res.data.unitId
    } else {
      newData.liveCommunityId = ''
      newData.liveBuildingId = ''
      newData.liveUnitId = ''
    }
  }
  if (newData.houseId && newData.houseId !== '') {
    let res1 = await getHouseInfo(newData.houseId)
    if (res1.code && res1.code === '200') {
      newData.communityId = res1.data.communityId
      newData.buildingId = res1.data.buildingId
      newData.unitId = res1.data.unitId
    } else {
      newData.communityId = ''
      newData.buildingId = ''
      newData.unitId = ''
    }
  }
  let formInfo = {}
  switch (tableName) {
    case '户籍人口':
      let collapse = {}
      collapse.activeName = '1'
      collapse.className = 'custom-collapse-style'
      collapse.items = [
        {
          title: '户籍基本信息',
          name: '1',
          formItem: [
            {label: '公民身份证号：', type: 'input', name: 'identityNumber'},
            {label: '出生日期：', type: 'datePicker', name: 'birthday'},
            {label: '姓名：', type: 'input', name: 'name'},
            {label: '曾用名：', type: 'input', name: 'usedName'},
            {label: '籍贯：', type: 'input', name: 'birthplace'},
            {
              label: '性别：',
              type: 'select',
              name: 'gender',
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('性别')
            },
            {
              label: '民族：',
              type: 'select',
              name: 'nation',
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('民族')
            },
            {
              label: '婚姻状况：',
              type: 'cascader',
              name: 'maritalStatus',
              showAllLevels: false,
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('婚姻状况')
            },
            {
              label: '政治面貌：',
              type: 'select',
              name: 'politicalStatus',
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('政治面貌')
            },
            {
              label: '学历：',
              type: 'cascader',
              name: 'education',
              showAllLevels: false,
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('学历')
            },
            {
              label: '宗教信仰：',
              type: 'select',
              name: 'religion',
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('宗教信仰')
            },
            {
              label: '职业类别：',
              type: 'select',
              name: 'careerCategory',
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('职业类别')
            },
            {label: '职业：', type: 'input', name: 'career'},
            {label: '服务处所：', type: 'input', name: 'servicePlace'},
            {label: '联系方式：', type: 'input', name: 'phone'},
            {label: '户籍地：', type: 'input', name: 'householdAddr'},
            {label: '现住地：', type: 'input', name: 'currentAddr'},
            {label: '现住地门（楼）详址：', type: 'input', name: 'currentAddrDetail'}
          ]
        },
        {
          title: '户籍地信息',
          name: '2',
          formItem: [
            {
              label: '户籍类型：',
              type: 'cascader',
              name: 'householdType',
              showAllLevels: false,
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('户籍类型')
            },
            {
              label: '网格：',
              type: 'cascader',
              name: 'gridId',
              showAllLevels: false,
              props: {label: 'name', value: 'id'},
              option: getUserGridTree()
            },
            {
              label: '居住地类型：',
              type: 'radioGroup',
              name: 'placeType',
              option: ['小区', '院落']
            },
            {
              label: '院落：',
              type: 'select',
              name: 'courtyardId',
              props: {label: 'name', value: 'id'},
              option: getYardOption(newData.gridId)
            },
            {
              label: '小区：',
              type: 'select',
              name: 'communityId',
              props: {label: 'name', value: 'id'},
              option: getCommunityOption(newData.gridId)
            },
            {
              label: '楼栋：',
              type: 'select',
              name: 'buildingId',
              props: {label: 'buildingName', value: 'id'},
              option: getBuildingOption(newData.communityId)
            },
            {
              label: '单元：',
              type: 'select',
              name: 'unitId',
              props: {label: 'unitName', value: 'id'},
              option: getUnitOption(newData.buildingId)
            },
            {
              label: '房屋：',
              type: 'select',
              name: 'houseId',
              props: {label: 'houseNumber', value: 'id'},
              option: getHouseOption(newData.unitId)
            },
            {
              label: '人户一致标识：',
              type: 'select',
              name: 'householdMark',
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('人户一致标识')
            },
            {
              label: '与户主关系：',
              type: 'cascader',
              name: 'houseHolderRelation',
              showAllLevels: false,
              props: {label: 'name', value: 'code'},
              option: getDictDataOption('家庭关系')
            },
            {label: '户主公民身份号码：', type: 'input', name: 'houseHolderIdentityNumber'},
            {label: '户主姓名：', type: 'input', name: 'houseHolderName'},
            {label: '户主联系方式：', type: 'input', name: 'houseHolderPhone'},
            {label: '户号：', type: 'input', name: 'householdNumber'}
          ]
        },
        {
          title: '居住地信息',
          name: '3',
          formItem: [
            {
              label: '居住类型：',
              type: 'select',
              name: 'stayType',
              props: {label: 'name', value: 'code'},
              option: [
                {name: '常住', code: '1'},
                {name: '流动', code: '2'}
              ]
            },
            {
              label: '网格：',
              type: 'cascader',
              name: 'liveGridId',
              showAllLevels: false,
              props: {label: 'name', value: 'id'},
              option: getUserGridTree()
            },
            {
              label: '现居住地类型：',
              type: 'radioGroup',
              name: 'livePlaceType',
              option: ['小区', '院落']
            },
            {
              label: '院落：',
              type: 'select',
              name: 'liveCourtyardId',
              props: {label: 'name', value: 'id'},
              option: getYardOption(data.liveGridId)
            },
            {
              label: '小区：',
              type: 'select',
              name: 'liveCommunityId',
              props: {label: 'name', value: 'id'},
              option: getCommunityOption(newData.liveGridId)
            },
            {
              label: '楼栋：',
              type: 'select',
              name: 'liveBuildingId',
              props: {label: 'buildingName', value: 'id'},
              option: getBuildingOption(newData.liveCommunityId)
            },
            {
              label: '单元：',
              type: 'select',
              name: 'liveUnitId',
              props: {label: 'unitName', value: 'id'},
              option: getUnitOption(newData.liveBuildingId)
            },
            {
              label: '房屋：',
              type: 'select',
              name: 'liveHouseId',
              props: {label: 'houseNumber', value: 'id'},
              option: getHouseOption(newData.liveUnitId)
            }
          ]
        }
      ]
      formInfo.formData = newData
      formInfo.collapse = collapse
      break
    default:
      break
  }
  console.log(formInfo)
  return formInfo
}
const yardRequst = id => {
  return new Promise(resolve => {
    findCourtyardList({likeAllGridId: id}).then(resp => {
      resolve(resp)
    })
  })
}
const getYardOption = async (id) => {
  let res = await yardRequst(id)
  if (res.code && res.code === '200') {
    return res.data
  } else {
    return []
  }
}
const communityRequst = id => {
  return new Promise(resolve => {
    findCommunityList({likeAllGridId: id}).then(resp => {
      resolve(resp)
    })
  })
}
const getCommunityOption = async (id) => {
  let res = await communityRequst(id)
  if (res.code && res.code === '200') {
    return res.data
  } else {
    return []
  }
}
const buildingRequst = id => {
  return new Promise(resolve => {
    findBuildingList({communityId: id}).then(resp => {
      resolve(resp)
    })
  })
}
const getBuildingOption = async (id) => {
  let res = await buildingRequst(id)
  if (res.code && res.code === '200') {
    return res.data
  } else {
    return []
  }
}
const unitRequst = id => {
  return new Promise(resolve => {
    findUnitList({buildingId: id}).then(resp => {
      resolve(resp)
    })
  })
}
const getUnitOption = async (id) => {
  let res = await unitRequst(id)
  if (res.code && res.code === '200') {
    return res.data
  } else {
    return []
  }
}
const houseRequst = id => {
  return new Promise(resolve => {
    findHouseList({unitId: id}).then(resp => {
      resolve(resp)
    })
  })
}
const getHouseOption = async (id) => {
  let res = await houseRequst(id)
  if (res.code && res.code === '200') {
    return res.data
  } else {
    return []
  }
}
